/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.blacklist;

import com.wtkj.vo.dzzf.base.kfwd.CardInfo;
import com.wtkj.vo.dzzf.base.kfwd.CompanyInfo;
import com.wtkj.vo.dzzf.base.kfwd.OBU;
import com.wtkj.vo.dzzf.base.kfwd.UserInfo;
import com.wttech.dzzf.process.constant.UserConstants;
import com.wttech.dzzf.process.dao.GenerateBlObuInfosDao;
import com.wttech.dzzf.process.dao.TransferUserInfoDao;
import com.wttech.dzzf.process.model.ObuTemp;
import com.wttech.dzzf.process.util.BusinessUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 生成obu信息子服务
 * @author MaShibo
 * @date 2017/12/22.
 */
@Service
public class GenerateBlObuInfoSubService {

	private static final Logger LOGGER = LoggerFactory.getLogger(GenerateBlObuInfoSubService.class);

	@Resource
	private TransferUserInfoDao transferUserInfoDao;

	@Resource
	private GenerateBlObuInfosDao generateBlObuInfosDao;

	void processObuTemp(ObuTemp obuTemp){
		String obuId = obuTemp.getObuId();
		OBU dzzfObu = generateBlObuInfosDao.queryDzzfObuById(obuId);
		if (dzzfObu == null){
			LOGGER.info("OBU 【{}】信息不存在", obuId);
			generateBlObuInfosDao.updateObuTempState(obuId, 3);
		}else {
			CardInfo cardInfo = generateBlObuInfosDao.queryCardInfoByVeh(dzzfObu.getVehiclePlate(), dzzfObu.getVehicleColor());
			if (cardInfo != null){
				String userId = cardInfo.getUserId();
				if (StringUtils.isNotBlank(userId)){
					generateBlObuInfosDao.updateObuTempUserId(obuId, userId);
				}else {
					Object certifiNoObj = cardInfo.getExtendProperties().get("USERIDNUM");
					Object certifiTypeObj = cardInfo.getExtendProperties().get("USERIDTYPE");
					Object ownerTypeObj = cardInfo.getExtendProperties().get("USERTYPE1");
					String certifiNo = null;
					Integer certifiType = null;
					Integer ownerType = null;
					if (certifiNoObj != null){
						certifiNo = certifiNoObj.toString();
					}
					if (certifiTypeObj != null){
						certifiType = Integer.valueOf(certifiTypeObj.toString());
					}
					if (ownerTypeObj != null){
						ownerType = Integer.valueOf(ownerTypeObj.toString());
					}
					if (certifiNo != null && certifiType != null && ownerType != null){
						Integer userIdType = BusinessUtils.convertUserIdType(certifiType, ownerType);
						if (userIdType == 199 || userIdType== 299){
							LOGGER.info("证件号【{}】证件类型【{}】用户类型【{}】错误", new Object[]{certifiNo, certifiType, ownerType});
						}else {
							if (ownerType == UserConstants.OLD_OWNERTYPE_USER){
								UserInfo query = new UserInfo();
								query.setUserIdNum(certifiNo);
								query.setUserIdType(userIdType);
								query = retrieveUserInfo(query);
								if (query != null){
									generateBlObuInfosDao.updateObuTempUserId(obuId, query.getId());
								}else {
									generateBlObuInfosDao.updateObuTempState(obuId, 3);
									LOGGER.info("证件号【{}】证件类型【{}】用户类型【{}】没有对应的客户信息", new Object[]{certifiNo, certifiType, ownerType});
								}
							}else if (ownerType == UserConstants.OLD_OWNERTYPE_COMPANY){
								CompanyInfo companyInfo = generateBlObuInfosDao.queryCompanyInfoByCert(certifiNo, certifiType, cardInfo.getTopClass());
								if (companyInfo != null){
									generateBlObuInfosDao.updateObuTempUserId(obuId, companyInfo.getId());
								}else {
									generateBlObuInfosDao.updateObuTempState(obuId, 3);
									LOGGER.info("证件号【{}】证件类型【{}】用户类型【{}】没有对应的客户信息", new Object[]{certifiNo, certifiType, ownerType});
								}
							}
						}
					}else {
						LOGGER.info("OBU 【{}】无法确定USERID", obuId);
						generateBlObuInfosDao.updateObuTempState(obuId, 3);
					}
				}
			}else {
				LOGGER.info("OBU 【{}】没有对应的卡片信息", obuId);
				generateBlObuInfosDao.updateObuTempState(obuId, 3);
			}
		}
	}

	private UserInfo retrieveUserInfo(UserInfo query){
		return (UserInfo) transferUserInfoDao.queryObject(query);
	}
}
